//地块数据
class FieldDataModel {
    private stageList = {};             //当前所有农作物的状态
    private fieldNum = 0;               //当前地块数量
    private fieldList = [];             //当前农作物数据
    private selectIndex = 0;            //当前选中的地块索引

    constructor() {
    }

    InitStage() {
        this.stageList = {}
        for (let i = 0; i < this.fieldList.length; i++) {
            //计算农作物当前阶段
            let fieldData = this.fieldList[i];
            let effectTime = 0; //道具施加在作物上影响的时间
            fieldData.propList.forEach(element => {
                if( Cfg.ItemCfg["prop_effect_time"]["value"]==Cfg.ItemCfg[element.itemId]["effect_type"])
                    effectTime += CtrlManager.GetCtrl(CtrlNames.Warehouse)[Cfg.ItemCfg[Cfg.ItemCfg[element.itemId]["effect_supply"]]["value"]](element.itemId) * element.number;
            });
            let stageTime = Math.floor(Cfg.ItemCfg[fieldData.cropId].time / Cfg.ItemCfg[fieldData.cropId].stage);
            let curStage = Math.floor((Game.serverTime - fieldData.plantTime + effectTime) / stageTime) + 1;
            if (curStage <= 0) {
                curStage = 1;
            }
            if (curStage > Cfg.ItemCfg[fieldData.cropId].stage+1) {
                curStage = Cfg.ItemCfg[fieldData.cropId].stage+1;
            }
            this.stageList[fieldData.index] = curStage;
        }

    }

    //获取所有农作物状态
    GetStageList() {
        return this.stageList;
    }

    //获取指定地块的农作物状态
    getStage(index:number) {
        return this.stageList[index];
    }

    //改变农作物状态
    ChangeState(index:number, stage:number) {
        this.stageList[index] = stage;
    }

    //种植
    Plant(index:number) {
        this.stageList[index] = 1;
    }

    //收获
    Harvest(index:number) {
        delete this.stageList[index];
    }

    SetFieldNum(fieldNum:number) {
        this.fieldNum = fieldNum;
    }

    GetFieldNum() {
        return this.fieldNum;
    }

    SetFieldList(fieldList:any) {
        this.fieldList = fieldList;
    }

    GetFieldList() {
        return this.fieldList;
    }

    GetFieldData(index:number) {
        for (let i = 0; i < this.fieldList.length; i++) {
            let fieldData = this.fieldList[i];
            if (index == fieldData.index) {
                return fieldData;
            }
        }
        return null;
    }

    GetFreeField() {
        for (let i = 0; i < this.fieldNum; i++) {
            let index = i+1
            let isFind = false
            for (let j = 0; j < this.fieldList.length; j++) {
                if (index == this.fieldList[j].index) {
                    isFind = true
                    break
                }
            }
            if (!isFind) {
                return index
            }
        }
        return 0
    }

    SetDisaster(index:number, disasterId:number) {
        for (let i = 0; i < this.fieldList.length; i++) {
            let fieldData = this.fieldList[i];
            if (index == fieldData.index) {
                fieldData.disasterId = disasterId;
                return;
            }
        }
    }

    //选中空地块
    SetSelected(index:number) {
        if (index > 0 && index <= Cfg.CommonCfg["fieldTotal"]) {
            this.selectIndex = index;   
        }
    }

    //清除选中状态
    ClearSelected() {
        this.selectIndex = 0;
    }

    GetSelected() {
        return this.selectIndex;
    }

    //指定玩家是否已经偷过指定农作物
    IsStealed(idnex:number, dbid:KBEngine.UINT64) : Boolean {
        let fieldData = this.GetFieldData(idnex);
        if (!fieldData) {
            return false;
        }
        for (let i in fieldData.stealList) {
            let stealDbid = fieldData.stealList[i];
            if (stealDbid.toString() == dbid.toString()) {
                return true;
            }
        }
        return false;
    }
}
let FieldModel = new FieldDataModel();